/*
* Poppy Game Engine - https://pgetemplate.googlecode.com/svn/
* -----------------------------------------------------------------------
* Licensed under the BSD license, see LICENSE in PSPSDK root for details.
*
* Copyright (c) 2008 Poppy
*/

#pragma once

#ifdef WIN32

#include <stdio.h>
#include <io.h>

#define PGE_FILE_READ_ONLY "rb"

#define PGE_FILE_WRITE_CREATE "wb"

#define PGE_FILE_APPEND "ab"

#define PGE_FILE_RW_OPEN "rb+"

#define PGE_FILE_RW_CREATE "wb+"

#define PGE_FILE_RW_APPEND "ab+"

#define PGE_FILE_HANDLE FILE*

#define PGE_FILE_FLAGS LPCSTR

#define PGE_FILE_SUCCEEDED(method) (method==0)

#define PGE_FILE_FAILED(method) (method==EOF)

#define PGE_FILE_OPEN(filename,mode) (::fopen(filename,mode))

#define PGE_FILE_CLOSE(handle) (::fclose(handle))

#define PGE_FILE_READ(handle,pointer,size) (::fread(pointer,1,size,handle))

#define PGE_FILE_WRITE(handle,pointer,size) (::fwrite(pointer,1,size,handle))

#define PGE_FILE_SEEK32(handle,offset,whence) (::fseek(handle,offset,whence))

#endif

#ifdef PSP

#include <pspiofilemgr.h>

#define PGE_FILE_READ_ONLY				PSP_O_RDONLY

#define PGE_FILE_WRITE_CREATE			PSP_O_WRONLY|PSP_O_CREAT

#define PGE_FILE_APPEND					PSP_O_APPEND|PSP_O_CREAT

#define PGE_FILE_READ_CREATE			PSP_O_RDONLY|PSP_O_CREAT

#define PGE_FILE_RW_OPEN				PSP_O_RDWR

#define PGE_FILE_RW_CREATE				PSP_O_RDWR|PSP_O_CREAT

#define PGE_FILE_RW_APPEND				PSP_O_RDWR|PSP_O_APPEND|PSP_O_CREAT

#define PGE_FILE_HANDLE SceUID

#define PGE_FILE_FLAGS SceMode

#define PGE_FILE_SUCCEEDED(method) (method>=0)

#define PGE_FILE_FAILED(method) (method<0)

#define PGE_FILE_OPEN(filename,mode) (sceIoOpen(filename,mode,0777))

#define PGE_FILE_CLOSE(handle) (sceIoClose(handle))

#define PGE_FILE_READ(handle,pointer,size) (sceIoRead(handle,pointer,size))

#define PGE_FILE_WRITE(handle,pointer,size) (sceIoWrite(handle,pointer,size))

#define PGE_FILE_SEEK32(handle,offset,whence) (sceIoLseek32(handle,offset,whence))

#define SEEK_SET PSP_SEEK_SET

#define SEEK_CUR PSP_SEEK_CUR

#define SEEK_END PSP_SEEK_END

#endif


enum PGE_WHENCE
{
	PGE_SEEK_SET = SEEK_SET,
	PGE_SEEK_CUR = SEEK_CUR,
	PGE_SEEK_END = SEEK_END
};

inline PGE_FILE_HANDLE PGE_OpenFile( LPCSTR filename, PGE_FILE_FLAGS flags )
{
	return PGE_FILE_OPEN(filename,flags);
}

inline PGE_FILE_HANDLE PGE_OpenFileRdCreate( LPCSTR filename )
{
	#ifdef WIN32
	if(::_access(filename, 0) < 0)
	{
		return PGE_FILE_OPEN(filename, PGE_FILE_WRITE_CREATE);
	}
	else
	{
		return PGE_FILE_OPEN(filename, PGE_FILE_READ_ONLY);
	}
	#endif
	#ifdef PSP
	return PGE_FILE_OPEN(filename,PGE_FILE_READ_CREATE);
	#endif
}

inline PGE_FILE_HANDLE PGE_OpenFileRWCreate( const char* filename )
{
	#ifdef WIN32
	if(_access(filename, 0) < 0)
	{
		return PGE_FILE_OPEN(filename, PGE_FILE_RW_CREATE);
	}
	else
	{
		return PGE_FILE_OPEN(filename, PGE_FILE_RW_OPEN);
	}
	#endif
	#ifdef PSP
	return PGE_FILE_OPEN(filename,PSP_O_RDWR|PSP_O_CREAT);
	#endif
}

inline bool PGE_CloseFile( PGE_FILE_HANDLE handle )
{
	return PGE_FILE_SUCCEEDED(PGE_FILE_CLOSE(handle));
}

inline int PGE_ReadBuffer( PGE_FILE_HANDLE handle, void* buffer, int size )
{
	return (int)PGE_FILE_READ(handle,buffer,size);
}

inline int PGE_WriteBuffer( PGE_FILE_HANDLE handle, void* buffer, int size )
{
	return (int)PGE_FILE_WRITE(handle,buffer,size);
}

inline int PGE_Seek32( PGE_FILE_HANDLE handle, int offset, PGE_WHENCE whence )
{
	return PGE_FILE_SEEK32(handle,offset,whence);
}
